package com.heima.serviceorder.service.impl;

import com.heima.Result;
import com.heima.order.Order;
import com.heima.product.Product;
import com.heima.serviceorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;
    @Override
    public Result<Order> createOrder(Long productId, Long userId) {

        Product product = getProductFormRemoteWithLoadBalanceAnnotation(productId);
        Order order = new Order();

        order.setId(1L);
        order.setTotalAmount(new BigDecimal("100"));
        order.setUserId(1L);
        order.setNickName("产品");
        order.setAddress("商业中心");

        order.setProductList(List.of(product));
        return Result.success(100,"获取成功",order);
    }
    private Product getProductFormRemoteWithLoadBalanceAnnotation(Long productId){
        String url="http://service-product/product/"+productId;
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }
}
